package top.jnaw.jee.platform.model;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.vdurmont.emoji.EmojiParser;
import top.jnaw.jee.platform.Consts.Result;
import top.jnaw.jee.platform.model.base.BaseWechatUsers;
import top.jnaw.jee.utils.Log;
import top.jnaw.jee.utils.ModelResult;
import top.jnaw.jee.utils.Models;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class WechatUsers extends BaseWechatUsers<WechatUsers> {

  public static final WechatUsers dao = new WechatUsers().dao();

  public static ModelResult save(JSONObject data) {
    ModelResult result = new ModelResult(Result.kModelSaveError);

    String nickname = data.getString("nickname");
    String headimg_url = data.getString("headimgurl");
    String openid = data.getString("openid");
    nickname = EmojiParser.parseToAliases(nickname);
    int sex = data.getInteger("sex");
    Log.e("微信的名字:" + nickname);
    String s = EmojiParser.parseToUnicode(nickname);
    Log.e("取的名字:" + s);
    try {
      WechatUsers user = dao
          .findFirst(Db.getSql("wechatUsers-find"), openid);

      boolean update = true;
      if (null == user) {
        user = new WechatUsers();
        update = false;
      }

      user.setNickname(nickname);
      user.setOpenid(openid);
      user.setHeadimgUrl(headimg_url);
      user.setSex(1 == sex ? "male" : (2 == sex ? "female" : "unknown"));
      user.setData(data.toJSONString());

      result.setResult((update ? user.update() : user.save())
                       ? Result.kSuccess
                       : Result.kModelUpdateError);
      result.setObject(user);

    } catch (Exception e) {
      e.printStackTrace();
      result.setResult(Result.kException);
    }

    return result;
  }

  public static ModelResult login(String name, String mobile) {
    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = new JSONObject();
    WechatUsers users = getByMobile(mobile);
    if (null != users) {
      users.setName(name);
      users.setMobile(mobile);
      users.setOpenid(mobile);
      boolean r = users.update();
      if (r) {
        result.setObject(object);
        result.setResult(Result.kSuccess);
      }
    } else {
      WechatUsers wechatUsers = new WechatUsers();
      wechatUsers.setName(name);
      wechatUsers.setMobile(mobile);
      wechatUsers.setOpenid(mobile);
      boolean r = wechatUsers.save();
      if (r) {
        result.setObject(object);
        result.setResult(Result.kSuccess);
      }
    }
    return result;
  }

  public static ModelResult bind(String openid, String name, String mobile) {
    ModelResult result = new ModelResult(Result.kNull);
    JSONObject object = new JSONObject();
    WechatUsers users = getByOpenid(openid);
    if (null != users) {
      users.setName(name);
      users.setMobile(mobile);
      boolean r = users.update();
      if (r) {
        object.put("is_bind", WechatUsers.isBind(openid));
        result.setObject(object);
        result.setResult(Result.kSuccess);
      }
    }
    return result;
  }

  public static WechatUsers getByMobile(String mobile) {
    return dao.findFirst(Db.getSql("wechatUsers-findMobile"), mobile);
  }

  public static WechatUsers getByOpenid(String openid) {
    return dao.findFirst(Db.getSql("wechatUsers-find"), openid);
  }

  public static boolean isBind(String openid) {
    boolean r = false;
    WechatUsers users = getByOpenid(openid);
    if (null != users) {
      String mobile = users.getMobile();
      String name = users.getName();
      if (StrKit.notBlank(name, mobile)) {
        r = true;
      }
    }
    return r;
  }

  public static JSONObject list(int page, int size, String keyWord) {

    String sec = Db.getSql("wechat_users_list-sec");
    String exp = Db.getSql("wechat_users_list-exp");
    if (StrKit.notBlank(keyWord)) {
      exp +=
          "\n" + "AND (wu.name like concat('%','" + keyWord
              + "','%') or  wu.nickname like concat('%','" + keyWord
              + "','%') or  wu.mobile like concat('%','" + keyWord
              + "','%'))";
    }
    //exp += " order by wu.id desc ";
    Page<Record> pages = Db.paginate(page, size, sec, exp);
    return Models.compressPage(pages);
  }

}
